<?php
class MessagePrives extends Model {
	
	private static $table_name = 'MESSAGE_PRIVE';
	
	public static function getMessages($idUser1, $idUser2) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT *
											FROM ".self::$table_name."
											WHERE (IDELEVE =:id1
											AND IDELEVE_DEST =:id2 )
											OR (IDELEVE =:id2
											AND IDELEVE_DEST =:id1 )
											ORDER BY DATEMESSAGE ASC"
										);
		$requete->execute(array('id1'=>$idUser1, 'id2'=>$idUser2));
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_ASSOC)) {
			$data[$i]=new MessagePrives($tab);
			$i++;
		}
		return $data;
	}
	
	public static function getConversations($id) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT * 
											FROM ".self::$table_name."  
											WHERE IDELEVE =:id
											OR IDELEVE_DEST=:id
											GROUP BY IDELEVE_DEST
											");
		$requete->execute(array('id'=>$id));
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_ASSOC)) {
			$data[$i]=new MessagePrives($tab);
			$i++;
		}
		return $data;
	}
	
	public function __construct($data) {
		parent::__construct();
		$this->values = $data;
	}
	
	public function getMessageId() { return $this->values['IDMESSAGEPRIVE']; }
	public function getSenderId() { return $this->values['IDELEVE']; }
	public function getReceiverId() { return $this->values['IDELEVE_DEST']; }
	public function getContenu() { return $this->values['CONTENUMESSAGE']; }
	public function estLu() { return $this->values['EST_LU']; }
	public function getDateMessage() { return $this->values['DATEMESSAGE']; }

	public function getSenderName() {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT PSEUDO
											FROM ELEVE
											WHERE IDELEVE=:id"
										);
		$requete->execute(array('id'=>$this->getSenderId()));
		$userName = $requete->fetch(PDO::FETCH_ASSOC);
		return $userName;
	}
	
	public function getReceiverName() {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT PSEUDO
											FROM ELEVE
											WHERE IDELEVE=:id"
										);
		$requete->execute(array('id'=>$this->getReceiverId()));
		$userName = $requete->fetch(PDO::FETCH_ASSOC);
		return $userName;
	}	
	// Créer un message privé
	public static function createMessagePrive($user,$user_dest,$contenu){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	INSERT INTO ".self::$table_name."(IDELEVE,IDELEVE_DEST,CONTENUMESSAGE,DATEMESSAGE)
											VALUES(:id,:id_dest,:contenu,NOW())
										");
		$requete->execute(array('id'=>$user,'id_dest'=>$user_dest,'contenu'=>$contenu));
	}
	
	public static function deleteConversationBetween($user,$user_dest) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	DELETE FROM ".self::$table_name." 
											WHERE (IDELEVE=:id AND IDELEVE_DEST=:id_dest)
											OR (IDELEVE_DEST=:id AND IDELEVE=:id_dest) 
										");
		$requete->execute(array('id'=>$user,'id_dest'=>$user_dest));
	}
	
	public static function readMessagesBetween($user,$user_dest) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ".self::$table_name."
											SET EST_LU=1
											WHERE (IDELEVE=:id AND IDELEVE_DEST=:id_dest)
											OR (IDELEVE_DEST=:id AND IDELEVE=:id_dest)
										");
		$requete->execute(array('id'=>$user,'id_dest'=>$user_dest));
	}
	
	public function getNumberOfPrivateMessages(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT COUNT(IDMESSAGEPRIVE)
											FROM MESSAGE_PRIVE
										");
		$requete->execute();
		$data = $requete->fetch(PDO::FETCH_ASSOC);
		return $data['COUNT(IDMESSAGEPRIVE)'];
	}
	

}